package cn.lu.controller;

import cn.lu.pojo.Chapter;
import cn.lu.service.ChapterService;
import cn.lu.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 章节
 */
@Controller
@RequestMapping("/chapter")
@ResponseBody
public class ChapterController {
    @Autowired
    private ChapterService chapterService;

    @RequestMapping(value = "/admin",method = RequestMethod.POST)
    public Result addInsert(@RequestBody Chapter chapter) {

        if (chapterService.addInsert(chapter) == 0){
            return new Result().fail("添加失败");
        }else{
            return new Result().success("添加成功");
        }


    }

    @RequestMapping(value = "",method = RequestMethod.GET)
    public Result selectByCurriculumId(@RequestParam(value = "curriculumId")Integer curriculumId) {
        return new Result().success(chapterService.selectByCurriculumId(curriculumId));
    }

    /*@RequestMapping(value = "",method = RequestMethod.PUT)
    public Result updateById(@RequestBody Chapter chapter) {
        return new Result().success(chapterService.updateById(chapter));
    }*/

    @RequestMapping(value = "",method = RequestMethod.PUT)
    public Result updateById(@RequestBody Chapter chapter,HttpServletRequest request) {
        return new Result().success(chapterService.updateByIdAndUserId(chapter,Integer.parseInt(request.getHeader("userId"))));
    }


    @RequestMapping(value = "",method = RequestMethod.DELETE)
    public Result sectionId(@RequestParam(value = "chapterId")Integer chapterId, HttpServletRequest request) {
        return chapterService.deleteByIdAndUserId(chapterId,Integer.parseInt(request.getHeader("userId")));
    }


    @RequestMapping(value = "",method = RequestMethod.POST)
    public Result addInTheUserId(@RequestBody Chapter chapter,HttpServletRequest request){
        return chapterService.addInTheUserId(chapter,Integer.parseInt(request.getHeader("userId")));
    }







}